﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls;

using LIM.forms.uc_CRM_HopDong_ChiTiet;
using LIM.model;
using LIM.controller;
using Telerik.WinControls.Data;
using LIM.report;

namespace LIM.forms
{
    public partial class radForm_CRM_HopDong_ChiTiet : Telerik.WinControls.UI.RadForm
    {
        public radForm_CRM_HopDong_ChiTiet()
        {
            InitializeComponent();
            //this.radLabel_NoteForDelete.AutoSize = false;
        }
        public radForm_CRM_HopDong_ChiTiet(int RowID)
        {
            InitializeComponent();
            //this.radLabel_NoteForDelete.AutoSize = false;
        }

        private void radForm_DanhMuc_PhongBan_Load(object sender, EventArgs e)
        {
            this.radLabel_SoTienConPhaiThu.AutoSize = false;
            this.radLabel_SoTienConPhaiThu.TextAlignment = ContentAlignment.MiddleRight;

            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem = new uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem();
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem.Dock = DockStyle.Fill;
            this.radPageViewPage_ChiTieuKiemNghiem.Controls.Add(uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem);

            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan uc_CRM_HopDong_ChiTiet_ThongTinBenNhan = new uc_CRM_HopDong_ChiTiet_ThongTinBenNhan();
            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan.Dock = DockStyle.Fill;
            this.radPageViewPage_ThongTinBenNhan.Controls.Add(uc_CRM_HopDong_ChiTiet_ThongTinBenNhan);

            uc_CRM_HopDong_ChiTiet_DieuKhoan uc_CRM_HopDong_ChiTiet_DieuKhoan = new uc_CRM_HopDong_ChiTiet_DieuKhoan();
            uc_CRM_HopDong_ChiTiet_DieuKhoan.Dock = DockStyle.Fill;
            this.radPageViewPage_DieuKhoan.Controls.Add(uc_CRM_HopDong_ChiTiet_DieuKhoan);

            uc_CRM_HopDong_ChiTiet_FileDinhKem uc_CRM_HopDong_ChiTiet_FileDinhKem = new uc_CRM_HopDong_ChiTiet_FileDinhKem();
            uc_CRM_HopDong_ChiTiet_FileDinhKem.Dock = DockStyle.Fill;
            this.radPageViewPage_FileDinhKem.Controls.Add(uc_CRM_HopDong_ChiTiet_FileDinhKem);

            uc_CRM_HopDong_ChiTiet_NhatKyLienLac uc_CRM_HopDong_ChiTiet_NhatKyLienLac = new uc_CRM_HopDong_ChiTiet_NhatKyLienLac();
            uc_CRM_HopDong_ChiTiet_NhatKyLienLac.Dock = DockStyle.Fill;
            this.radPageViewPage_NhatKyLienLac.Controls.Add(uc_CRM_HopDong_ChiTiet_NhatKyLienLac);


            maximumSizeOfPanel1 = splitPanel1.Height;

            //\\VNThanh
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem.Name = "uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem";
            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan.Name = "uc_CRM_HopDong_ChiTiet_ThongTinBenNhan";
            uc_CRM_HopDong_ChiTiet_DieuKhoan.Name = "uc_CRM_HopDong_ChiTiet_DieuKhoan";
            VNThanhLoad();
        }

        private void radMenuItem_TaoPhieuThu_Click(object sender, EventArgs e)
        {
            radForm_CRM_PhieuThu_ChiTiet form = new radForm_CRM_PhieuThu_ChiTiet();
            form.MdiParent = this.MdiParent;
            form.Show();
        }

        int minimumSizeOfPanel1 = 30;
        int maximumSizeOfPanel1 = 204;
        private void ZoomInOut_Click(object sender, EventArgs e)
        {
            if (splitPanel1.Height == minimumSizeOfPanel1)
            {
                this.splitPanel1.SizeInfo.AbsoluteSize = new Size(this.splitPanel1.Width, maximumSizeOfPanel1);
                this.radButton_ZoomIn.BringToFront();
            }
            else
            {
                this.splitPanel1.SizeInfo.AbsoluteSize = new Size(this.splitPanel1.Width, minimumSizeOfPanel1);
                this.radButton_ZoomOut.BringToFront();
            }

            //this.splitPanel1.Visible = !this.splitPanel1.Visible;
        }

        //\\VNThanh
        void VNThanhLoad()
        {
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
            ucChiTieuKiemNghiem.EvtUserAddedRow += new Telerik.WinControls.UI.ValueChangingEventHandler(uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem_EvtUserAddedRow);
            this.radDropDownList2.SelectedIndexChanged += new EventHandler(radDropDownList2_SelectedIndexChanged);
            this.radMenuItem1.Click += new EventHandler(radMenuItem1_Click);
            this.radMenuItem2.Click += new EventHandler(radMenuItem2_Click);
            this.radMenuItem3.Click += new EventHandler(radMenuItem3_Click);

            LoadKhachHangComboBox();
            LoadTinhTrangComboBox();

            ResetControlValues();

            //// Test Update HopDong.
            //tbl_CRM_HopDong otbl_CRM_HopDong = Controller.GetHopDong(-1);
            //SetControlValues(otbl_CRM_HopDong);
        }

        private DateTime _CurrentDate;
        public DateTime CurrentDate
        {
            get { if (_CurrentDate == default(DateTime)) _CurrentDate = Controller.GetCurrentDateTimeFromDatabaseServer(); return _CurrentDate; }
            set { _CurrentDate = value; }
        }
        private string CreatedBy = InitUserInfos.UserName;
        public int RowIDHopDong { get; set; }

        private LIM.controller.Thanh_CRMHopDongChiTietController _Controller;
        public LIM.controller.Thanh_CRMHopDongChiTietController Controller
        {
            get { if (_Controller == null) _Controller = new Thanh_CRMHopDongChiTietController(); return _Controller; }
            set { _Controller = value; }
        }

        System.Windows.Forms.BindingSource _KhachHangBindingSource = new BindingSource();
        System.Windows.Forms.BindingSource _TinhTrangBindingSource = new BindingSource();

        bool IsKHLoaded = false;
        // Load Khach hang
        void LoadKhachHangComboBox()
        {
            // Lay danh sach khach hang.
            List<Thanh_KhachHang> lstKhachHang = Controller.GetsKhachHang().ToList();
            _KhachHangBindingSource.DataSource = lstKhachHang;

            radDropDownList2.DataSource = null;

            // Gan danh sach vao combobox.
            if (lstKhachHang.Count > 0)
            {
                IsKHLoaded = false;
                this.radDropDownList2.DisplayMember = "TenGoi";
                this.radDropDownList2.ValueMember = "RowID";
                this.radDropDownList2.DataSource = _KhachHangBindingSource;
                // Set height layout.
                this.radDropDownList2.MultiColumnComboBoxElement.DropDownSizingMode = Telerik.WinControls.UI.SizingMode.UpDownAndRightBottom;
                this.radDropDownList2.MultiColumnComboBoxElement.DropDownMinSize = new Size(0, 200);
                this.radDropDownList2.MultiColumnComboBoxElement.AutoSizeDropDownToBestFit = true;
                //Filter MaKhachHang
                FilterDescriptor oFilterDescriptorMaKhachHang = new FilterDescriptor("MaKhachHang", FilterOperator.Contains, "");
                oFilterDescriptorMaKhachHang.IsFilterEditor = true;
                this.radDropDownList2.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorMaKhachHang);
                //this.radDropDownList2.MultiColumnComboBoxElement.EditorControl.Columns["MaKhachHang"].FilterDescriptor = oFilterDescriptorMaKhachHang;

                //Filter TenGoi
                FilterDescriptor oFilterDescriptorTenGoi = new FilterDescriptor("TenGoi", FilterOperator.Contains, "");
                oFilterDescriptorTenGoi.IsFilterEditor = true;
                this.radDropDownList2.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorTenGoi);
                //this.radDropDownList2.MultiColumnComboBoxElement.EditorControl.Columns["TenGoi"].FilterDescriptor = oFilterDescriptorTenGoi;

                //Filter TenGoiNgan
                FilterDescriptor oFilterDescriptorTenGoiNgan = new FilterDescriptor("TenGoiNgan", FilterOperator.Contains, "");
                oFilterDescriptorTenGoiNgan.IsFilterEditor = true;
                this.radDropDownList2.EditorControl.MasterTemplate.FilterDescriptors.Add(oFilterDescriptorTenGoiNgan);
                //this.radDropDownList2.MultiColumnComboBoxElement.EditorControl.Columns["TenGoiNgan"].FilterDescriptor = oFilterDescriptorTenGoiNgan;

                this.radDropDownList2.EditorControl.MasterTemplate.FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;

                this.radDropDownList2.MultiColumnComboBoxElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.radDropDownList2.DropDownStyle = RadDropDownStyle.DropDown;
                this.radDropDownList2.EditorControl.ShowFilteringRow = true;
                this.radDropDownList2.AutoFilter = true;

                // Xu ly header cua GridView trong ComboBox.
                // "MaKhachHang":
                radDropDownList2.EditorControl.Columns["MaKhachHang"].HeaderText = "Mã khách hàng";
                radDropDownList2.EditorControl.Columns["MaKhachHang"].HeaderTextAlignment = ContentAlignment.MiddleCenter;
                radDropDownList2.EditorControl.Columns["MaKhachHang"].BestFit();
                // "TenGoi":
                radDropDownList2.EditorControl.Columns["TenGoi"].HeaderText = "Tên gọi";
                radDropDownList2.EditorControl.Columns["TenGoi"].HeaderTextAlignment = ContentAlignment.MiddleCenter;
                radDropDownList2.EditorControl.Columns["TenGoi"].BestFit();
                // "TenGoiNgan":
                radDropDownList2.EditorControl.Columns["TenGoiNgan"].HeaderText = "Tên gọi ngắn";
                radDropDownList2.EditorControl.Columns["TenGoiNgan"].HeaderTextAlignment = ContentAlignment.MiddleCenter;
                radDropDownList2.EditorControl.Columns["TenGoiNgan"].BestFit();
                // "RowID":
                radDropDownList2.EditorControl.Columns["RowID"].IsVisible = false;
                radDropDownList2.EditorControl.Columns["DiaChi"].IsVisible = false;
                radDropDownList2.EditorControl.Columns["SoDienThoai"].IsVisible = false;
                radDropDownList2.EditorControl.Columns["SoFax"].IsVisible = false;

                this.radDropDownList2.SelectedIndex = -1;
                IsKHLoaded = true;
            }
        }

        // Load Tinh Trang
        void LoadTinhTrangComboBox()
        {
            // Lay danh sach tinh trang.
            List<Thanh_TinhTrangComboBox> lstTinhTrang = Controller.GetsTinhTrangComboBox().ToList();
            _TinhTrangBindingSource.DataSource = lstTinhTrang;
            radDropDownList3.SelectedIndex = -1;
            radDropDownList3.DataSource = null;
            if (lstTinhTrang.Count > 0)
            {
                radDropDownList3.DisplayMember = "TenGoi";
                radDropDownList3.ValueMember = "MaDanhMuc";
                radDropDownList3.DataSource = _TinhTrangBindingSource;
                radDropDownList3.SelectedIndex = 0;
            }
        }

        // Set gia tri mac dinh cho control
        void ResetControlValues()
        {
            this.radLabel_SoTienConPhaiThu.Text = "0";
            this.radDateTimePicker1.Value = this.radDateTimePicker2.Value = this.radDateTimePicker3.Value = CurrentDate;
            this.radTxtGhiChu.Text = this.radTxtMaHopDong.Text = string.Empty;
            this.radLblNgayNhap.Text = "Ngày nhập vào hệ thống: " + CurrentDate.ToString("dd/MM/yyyy HH:ss");
            this.radLblNguoiNhap.Text = "Nhận viên nhập: " + InitUserInfos.UserName;
        }

        // Set gia tri control tu Hop dong id.
        public void SetControlValues(int pHopDongRowID)
        {
            tbl_CRM_HopDong otbl_CRM_HopDong = Controller.GetHopDong(pHopDongRowID);
            SetControlValues(otbl_CRM_HopDong);
        }

        // Set gia tri control tu doi tuong tbl_CRM_HopDong.
        void SetControlValues(tbl_CRM_HopDong ptbl_CRM_HopDong)
        {
            if (ptbl_CRM_HopDong != null)
            {
                RowIDHopDong = ptbl_CRM_HopDong.RowID;
                uc_CRM_HopDong_ChiTiet_ThongTinBenNhan ucThongTinBenNhan = this.radPageViewPage_ThongTinBenNhan.Controls["uc_CRM_HopDong_ChiTiet_ThongTinBenNhan"] as uc_CRM_HopDong_ChiTiet_ThongTinBenNhan;
                uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
                uc_CRM_HopDong_ChiTiet_DieuKhoan ucDieuKhoanThanhToan = this.radPageViewPage_DieuKhoan.Controls["uc_CRM_HopDong_ChiTiet_DieuKhoan"] as uc_CRM_HopDong_ChiTiet_DieuKhoan;

                Thanh_HopDong oThanh_HopDong = new Thanh_HopDong();
                Util.CopyValues(ptbl_CRM_HopDong, oThanh_HopDong, false);

                this.radDropDownList2.SelectedValue = oThanh_HopDong.RowIDKhachHang;
                ucThongTinBenNhan.SetControlValues(oThanh_HopDong);

                List<Thanh_ChiTieuKiemNghiem> oLstThanh_ChiTieuKiemNghiem = new List<Thanh_ChiTieuKiemNghiem>();
                foreach (var item in ptbl_CRM_HopDong.tbl_CRM_HopDong_ChiTieuKiemNghiem.ToList())
                {
                    Thanh_ChiTieuKiemNghiem oThanh_ChiTieuKiemNghiem = new Thanh_ChiTieuKiemNghiem();
                    Util.CopyValues(item, oThanh_ChiTieuKiemNghiem, false);
                    oLstThanh_ChiTieuKiemNghiem.Add(oThanh_ChiTieuKiemNghiem);
                }
                ucChiTieuKiemNghiem.SetControlValues(oLstThanh_ChiTieuKiemNghiem);
                ucChiTieuKiemNghiem.SoTang = oThanh_HopDong.SoTang;
                ucChiTieuKiemNghiem.SoGiam = oThanh_HopDong.SoGiam;

                List<Thanh_DieuKhoanThanhToan> oLstThanh_DieuKhoanThanhToan = new List<Thanh_DieuKhoanThanhToan>();
                foreach (var item in ptbl_CRM_HopDong.tbl_CRM_HopDong_DieuKhoanThanhToan.ToList())
                {
                    Thanh_DieuKhoanThanhToan oThanh_DieuKhoanThanhToan = new Thanh_DieuKhoanThanhToan();
                    Util.CopyValues(item, oThanh_DieuKhoanThanhToan, false);
                    oLstThanh_DieuKhoanThanhToan.Add(oThanh_DieuKhoanThanhToan);
                }
                ucDieuKhoanThanhToan.SetControlValues(oLstThanh_DieuKhoanThanhToan);

                this.radDateTimePicker1.Value = oThanh_HopDong.ThoiGianHieuLuc_Tu;
                this.radDateTimePicker2.Value = oThanh_HopDong.ThoiGianHieuLuc_Den;
                this.radDateTimePicker3.Value = oThanh_HopDong.NgayKy;
                this.radTxtGhiChu.Text = oThanh_HopDong.GhiChu;
                this.radTxtMaHopDong.Text = oThanh_HopDong.MaHopDong;

                this.radDropDownList3.SelectedValue = oThanh_HopDong.TinhTrang;
                this.radLblNgayNhap.Text = "Ngày nhập vào hệ thống: " + oThanh_HopDong.NgayCapNhat.ToString("dd/MM/yyyy HH:ss");
                this.radLblNguoiNhap.Text = "Nhận viên nhập: " + oThanh_HopDong.NguoiCapNhat;

                CreatedBy = oThanh_HopDong.NguoiTao;
            }
        }

        // Tao moi hop dong tu bao gia.
        public void CreateHopDong(Thanh_BaoGia pThanh_BaoGia, List<Thanh_ChiTieuKiemNghiem> pLstThanh_ChiTieuKiemNghiem)
        {
            if (pThanh_BaoGia != null)
            {
                RowIDHopDong = 0;
                uc_CRM_HopDong_ChiTiet_ThongTinBenNhan ucThongTinBenNhan = this.radPageViewPage_ThongTinBenNhan.Controls["uc_CRM_HopDong_ChiTiet_ThongTinBenNhan"] as uc_CRM_HopDong_ChiTiet_ThongTinBenNhan;
                uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
                uc_CRM_HopDong_ChiTiet_DieuKhoan ucDieuKhoanThanhToan = this.radPageViewPage_DieuKhoan.Controls["uc_CRM_HopDong_ChiTiet_DieuKhoan"] as uc_CRM_HopDong_ChiTiet_DieuKhoan;

                Thanh_HopDong oThanh_HopDong = new Thanh_HopDong();
                Util.CopyValues(pThanh_BaoGia, oThanh_HopDong, false);

                this.radDropDownList2.SelectedValue = oThanh_HopDong.RowIDKhachHang;
                ucThongTinBenNhan.SetControlValues(oThanh_HopDong);

                ucChiTieuKiemNghiem.SetControlValues(pLstThanh_ChiTieuKiemNghiem);

                List<Thanh_DieuKhoanThanhToan> oLstThanh_DieuKhoanThanhToan = new List<Thanh_DieuKhoanThanhToan>();
                ucDieuKhoanThanhToan.SetControlValues(oLstThanh_DieuKhoanThanhToan);

                this.radDateTimePicker1.Value = oThanh_HopDong.ThoiGianHieuLuc_Tu;
                this.radDateTimePicker2.Value = oThanh_HopDong.ThoiGianHieuLuc_Den;
                this.radDateTimePicker3.Value = CurrentDate;
//                this.radDateTimePicker3.Value = oThanh_HopDong.NgayKy;
                this.radTxtGhiChu.Text = oThanh_HopDong.GhiChu;
                this.radTxtMaHopDong.Text = oThanh_HopDong.MaHopDong;

                this.radDropDownList3.SelectedValue = oThanh_HopDong.TinhTrang;
                this.radLblNgayNhap.Text = "Ngày nhập vào hệ thống: " + oThanh_HopDong.NgayCapNhat.ToString("dd/MM/yyyy HH:ss");
                this.radLblNguoiNhap.Text = "Nhận viên nhập: " + oThanh_HopDong.NguoiCapNhat;

                CreatedBy = oThanh_HopDong.NguoiTao;
            }
        }

        // Refresh
        public void RefreshControl()
        {
            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan ucThongTinBenNhan = this.radPageViewPage_ThongTinBenNhan.Controls["uc_CRM_HopDong_ChiTiet_ThongTinBenNhan"] as uc_CRM_HopDong_ChiTiet_ThongTinBenNhan;
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
            ucThongTinBenNhan.RefreshControl();
            ucChiTieuKiemNghiem.RefreshControl();
        }
        void radMenuItem1_Click(object sender, EventArgs e)
        {
            RefreshControl();
        }

        // Check Input values.
        bool IsInputValid()
        {
            if (this.radDropDownList2.SelectedIndex < 0)
                return false;
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
            if (ucChiTieuKiemNghiem.LstThanh_ChiTieuKiemNghiem.Count < 0)
                return false;
            return true;
        }

        // Save
        void radMenuItem2_Click(object sender, EventArgs e)
        {
            if (!IsInputValid())
            {
                MessageBox.Show("Thông tin nhập vào chưa đầy đủ.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            uc_CRM_HopDong_ChiTiet_ThongTinBenNhan ucThongTinBenNhan = this.radPageViewPage_ThongTinBenNhan.Controls["uc_CRM_HopDong_ChiTiet_ThongTinBenNhan"] as uc_CRM_HopDong_ChiTiet_ThongTinBenNhan;
            uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem ucChiTieuKiemNghiem = this.radPageViewPage_ChiTieuKiemNghiem.Controls["uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem"] as uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem;
            uc_CRM_HopDong_ChiTiet_DieuKhoan ucDieuKhoan = this.radPageViewPage_DieuKhoan.Controls["uc_CRM_HopDong_ChiTiet_DieuKhoan"] as uc_CRM_HopDong_ChiTiet_DieuKhoan;
            Thanh_HopDong oThanh_HopDong = ucThongTinBenNhan.GetControlValues();
            oThanh_HopDong.SoTang = ucChiTieuKiemNghiem.SoTang;
            oThanh_HopDong.SoGiam = ucChiTieuKiemNghiem.SoGiam;

            oThanh_HopDong.ThoiGianHieuLuc_Tu = this.radDateTimePicker1.Value;
            oThanh_HopDong.ThoiGianHieuLuc_Den = this.radDateTimePicker2.Value;
            oThanh_HopDong.NgayKy = this.radDateTimePicker3.Value;
            oThanh_HopDong.GhiChu = this.radTxtGhiChu.Text;
            oThanh_HopDong.MaHopDong = this.radTxtMaHopDong.Text;
            oThanh_HopDong.TinhTrang = this.radDropDownList3.SelectedValue == null ? "" : this.radDropDownList3.SelectedValue.ToString();
            Controller.SaveHopDong(oThanh_HopDong, ucChiTieuKiemNghiem.LstThanh_ChiTieuKiemNghiem, ucDieuKhoan.LstThanh_DieuKhoanThanhToan);
            this.radTxtMaHopDong.Text = oThanh_HopDong.MaHopDong;
            RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Báo giá", "Đã lưu thành công!");
            //MessageBox.Show("Thông tin báo giá đã được lưu.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        // Xoa
        void radMenuItem3_Click(object sender, EventArgs e)
        {
            if (RowIDHopDong != default(int))
            {
                if (CreatedBy != InitUserInfos.UserName)
                {
                    MessageBox.Show("Bạn không phải là người tạo hợp đồng này.\r\nNên bạn không thể xóa.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("Bạn có chắc muốn xóa báo giá này không?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    Controller.DeleteHopDong(RowIDHopDong);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Thông tin báo giá chưa được lưu.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Chon KH tu combobox.
        void radDropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsKHLoaded)
            {
                uc_CRM_HopDong_ChiTiet_ThongTinBenNhan uc = this.radPageViewPage_ThongTinBenNhan.Controls["uc_CRM_HopDong_ChiTiet_ThongTinBenNhan"] as uc_CRM_HopDong_ChiTiet_ThongTinBenNhan;
                Thanh_KhachHang oThanh_KhachHang = null;
                if (radDropDownList2.SelectedItem != null)
                {
                    oThanh_KhachHang = ((Telerik.WinControls.UI.GridViewRowInfo)(radDropDownList2.SelectedItem)).DataBoundItem as Thanh_KhachHang;
                }
                uc.SetControlValues(oThanh_KhachHang);
            }
        }

        // Hander EvtUserAddedRow tu control KiemNghiem.
        void uc_CRM_HopDong_ChiTiet_ChiTieuKiemNghiem_EvtUserAddedRow(object sender, Telerik.WinControls.UI.ValueChangingEventArgs e)
        {
            this.radLabel_SoTienConPhaiThu.Text = (decimal.Parse(this.radLabel_SoTienConPhaiThu.Text) + decimal.Parse(e.NewValue.ToString()) - decimal.Parse(e.OldValue.ToString())).ToString("#,###");
        }

        // Gia tri tu ngay thay doi.
        private void radDateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (radDateTimePicker1.Value.Date > radDateTimePicker2.Value.Date)
            {
                MessageBox.Show("Từ ngày không thể lớn hơn đến ngày.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                radDateTimePicker1.Value = radDateTimePicker2.Value;
            }
        }

        // Gia tri den ngay thay doi.
        private void radDateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (radDateTimePicker2.Value.Date < radDateTimePicker1.Value.Date)
            {
                MessageBox.Show("Đến ngày không thể nhỏ hơn từ ngày.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                radDateTimePicker2.Value = radDateTimePicker1.Value;
            }
        }

        private void radMenuItem_In_Click(object sender, EventArgs e)
        {
            frm_RPHDDVKHCN frm = new frm_RPHDDVKHCN();
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }
    }
}
